package math.geom3d;

import math.base.CCS3d;
import math.base.Coord3d;
import math.base.Point3d;
import math.base.Vector3d;
import math.geomUtils.CurveType;

/**
 * @author Saga
 *
 */
public class Hyperbola3d extends ConicalCurve3d {
    public Hyperbola3d(final CCS3d _pos, final double _majorRadius, final double _minorRadius) {
        this.pos = _pos;
        this.majorRadius = _majorRadius;
        this.minorRadius = _minorRadius;
    }

    @Override
    public final CurveType getCurveType() {
        return CurveType.HYPERBOLA;
    }

    @Override
    public final void reverse() {
        this.pos.yReverse();
    }

    @Override
    public final Curve3d reversed() {
        return new Hyperbola3d(this.pos.yReversed(), this.majorRadius, this.minorRadius);
    }

    @Override
    public final boolean isPeriodic() {
        return false;
    }

    @Override
    public final double getPeriod() {
        throw new ArithmeticException("");
    }

    @Override
    public final double getFirstParameter() {
        return Double.NEGATIVE_INFINITY;
    }

    @Override
    public final double getLastParameter() {
        return Double.POSITIVE_INFINITY;
    }

    public final void setMajorRadius(final double _majorRadius) {
        this.majorRadius = _majorRadius;
    }

    public final double getMajorRadius() {
        return this.majorRadius;
    }

    public final void setMinorRadius(final double _minorRadius) {
        this.minorRadius = _minorRadius;
    }

    public final double getMinorRadius() {
        return this.minorRadius;
    }

    @Override
    public final Point3d calcValue(final double _param) {
        final double cosVal = Math.cos(_param) * this.majorRadius;
        final double sinVal = Math.sin(_param) * this.minorRadius;
        return this.pos.getOrigin().added(this.pos.getDirectX().multiplied(cosVal)).added(
                this.pos.getDirectY().multiplied(sinVal));
    }

    @Override
    public final Vector3d calcDerivative(final double _param) {
        final double coshVal = Math.cosh(_param);
        final double sinhVal = Math.sinh(_param);

        return this.pos.getDirectX().multiplied(sinhVal * this.majorRadius)
                .added(this.pos.getDirectY().multiplied(coshVal * this.minorRadius));
    }

    @Override
    public final Vector3d calcDerivativeN(final double _param, final int _order) {
        if (_order < 1) {
            throw new ArithmeticException("");
        }

        final double coshVal = Math.cosh(_param);
        final double sinhVal = Math.sinh(_param);

        if (_order % 2 == 0) {
            return this.pos.getDirectX().multiplied(coshVal * this.majorRadius)
                    .added(this.pos.getDirectY().multiplied(sinhVal * this.minorRadius));
        } else {
            return this.pos.getDirectX().multiplied(sinhVal * this.majorRadius)
                    .added(this.pos.getDirectY().multiplied(coshVal * this.minorRadius));
        }
    }

    @Override
    public final void calcValueAndDerivative1(final double _param, final Point3d _pt,
            final Vector3d _der1) {
        final double coshVal = Math.cosh(_param);
        final double sinhVal = Math.sinh(_param);

        _pt.setCoord(this.pos.getOrigin().getCoord()
                .added(this.pos.getDirectX().getCoord().multiplied(coshVal * this.majorRadius))
                .added(this.pos.getDirectY().getCoord().multiplied(sinhVal * this.minorRadius)));
        _der1.setCoord(this.pos.getDirectX().getCoord().multiplied(sinhVal * this.majorRadius)
                .added(this.pos.getDirectY().getCoord().multiplied(coshVal * this.minorRadius)));
    }

    @Override
    public final void calcValueAndDerivative2(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2) {
        final double coshVal = Math.cosh(_param);
        final double sinhVal = Math.sinh(_param);

        final Coord3d coord = this.pos.getDirectX().getCoord().multiplied(
                coshVal * this.majorRadius).added(
                this.pos.getDirectY().getCoord().multiplied(sinhVal * this.minorRadius));
        final Coord3d derCoord = this.pos.getDirectX().getCoord().multiplied(
                sinhVal * this.majorRadius).added(
                this.pos.getDirectY().getCoord().multiplied(coshVal * this.minorRadius));

        _pt.setCoord(this.pos.getOrigin().getCoord().added(coord));
        _der1.setCoord(derCoord);
        _der2.setCoord(coord);
    }

    @Override
    public final void calcValueAndDerivative3(final double _param, final Point3d _pt,
            final Vector3d _der1, final Vector3d _der2, final Vector3d _der3) {
        final double coshVal = Math.cosh(_param);
        final double sinhVal = Math.sinh(_param);

        final Coord3d coord = this.pos.getDirectX().getCoord().multiplied(
                coshVal * this.majorRadius).added(
                this.pos.getDirectY().getCoord().multiplied(sinhVal * this.minorRadius));
        final Coord3d derCoord = this.pos.getDirectX().getCoord().multiplied(
                sinhVal * this.majorRadius).added(
                this.pos.getDirectY().getCoord().multiplied(coshVal * this.minorRadius));

        _pt.setCoord(this.pos.getOrigin().getCoord().added(coord));
        _der1.setCoord(derCoord);
        _der2.setCoord(coord);
        _der3.setCoord(derCoord);
    }

    @Override
    public final Hyperbola3d clone() {
        return new Hyperbola3d(this.pos.clone(), this.majorRadius, this.minorRadius);
    }

    private double majorRadius;
    private double minorRadius;
}
